﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



using MBSDAL;

namespace MBSBusinessEntities
{
    public class MovieScheduleBE : ICrud
    {

        public MovieScheduleBE()
        {
            this.mbs = new MBS_Entities();
        }

        public MovieScheduleBE(
        DateTime StartDate,
        DateTime EndDate,
        TimeSpan ShowStartTime,
        TimeSpan ShowEndTime,
        double WeekDateFare,
        double WeekEndFare,
        bool IsDeleted,
        int CinemaID,
        int ScreenNo,
        int MovieID)
        {
            this.mbs = new MBS_Entities();
            this.StartDate = StartDate;
            this.EndDate = EndDate;
            this.ShowStartTime = ShowStartTime;
            this.ShowEndTime = ShowEndTime;
            this.WeekDateFare = WeekDateFare;
            this.WeekEndFare = WeekEndFare;
            this.IsDeleted = IsDeleted;
            this.CinemaID = CinemaID;
            this.ScreenNo = ScreenNo;
            this.MovieID = MovieID;
        }

        #region ClassVariables

        private MBS_Entities mbs;
        private int movieScheduleID;
        private DateTime startDate;
        private DateTime endDate;
        private TimeSpan showStartTime;
        private TimeSpan showEndTime;
        private double weekDateFare;
        private double weekEndFare;
        private bool isDeleted;
        private int cinemaID;
        private int screenNo;
        private int movieID;

        private bool isValid = true;

        public bool IsValid
        {
            get { return isValid; }
            set { isValid = value; }
        }
        private string errorMessage = string.Empty;

        #endregion


        #region Properties

        public MBS_Entities Mbs
        {
            get { return mbs; }
            set { mbs = value; }
        }

        public int MovieScheduleID
        {
            get { return movieScheduleID; }
            set { movieScheduleID = value; }
        }

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }

        public TimeSpan ShowStartTime
        {
            get { return showStartTime; }
            set { showStartTime = value; }
        }

        public TimeSpan ShowEndTime
        {
            get { return showEndTime; }
            set { showEndTime = value; }
        }

        public double WeekDateFare
        {
            get { return weekDateFare; }
            set { weekDateFare = value; }
        }

        public double WeekEndFare
        {
            get { return weekEndFare; }
            set { weekEndFare = value; }
        }

        public bool IsDeleted
        {
            get { return isDeleted; }
            set { isDeleted = value; }
        }

        public int CinemaID
        {
            get { return cinemaID; }
            set { cinemaID = value; }
        }

        public int ScreenNo
        {
            get { return screenNo; }
            set { screenNo = value; }
        }

        public int MovieID
        {
            get { return movieID; }
            set { movieID = value; }
        }



        #endregion


        #region Methods

        public bool Add()
        {
            try
            {
            MovieSchedule movieSchedule = new MovieSchedule();
            movieSchedule.CinemaID = this.CinemaID;
            movieSchedule.EndDate = this.EndDate.Date;
            movieSchedule.IsDeleted = this.IsDeleted;
            movieSchedule.MovieID = this.MovieID;
            movieSchedule.ScreenNo = this.ScreenNo;
            movieSchedule.ShowEndTime = this.ShowEndTime;
            movieSchedule.ShowStartTime = this.ShowStartTime;
            movieSchedule.StartDate = this.StartDate.Date;
            movieSchedule.WeekDateFare = Convert.ToDecimal(this.WeekDateFare);
            movieSchedule.WeekEndFare = Convert.ToDecimal(this.WeekEndFare);
            mbs.AddToMovieSchedules(movieSchedule);
            mbs.SaveChanges();

            int diff = (this.EndDate - this.StartDate).Days + 1;

            DateTime date = this.StartDate;
            while (date <= this.EndDate)
            {
                ShowTime showTime = new ShowTime();
                showTime.Fare = Convert.ToDecimal((date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) ? this.WeekEndFare : this.weekDateFare);
                showTime.ShowDateTime = new DateTime(date.Year, date.Month, date.Day, this.ShowStartTime.Hours, this.ShowStartTime.Minutes, this.ShowStartTime.Seconds);
                showTime.SeatsAvailable = generateAvailableSeats();
                showTime.MovieScheduleID = movieSchedule.MovieScheduleID; ;
                date = date.AddDays(1);
                mbs.AddToShowTimes(showTime);
                mbs.SaveChanges();
            }

  

            }
            catch (System.Data.UpdateException ex)
            {
                //Duplicate Cinemaname exists in the database.
                return false;
            }

            return true;
        }

        private string generateAvailableSeats()
        {
            int noOfRows = 5;
            int noOfCols = 8;

            StringBuilder sb = new StringBuilder();
            sb.Append("<seatLayout>");
            for (int i = 0; i < noOfRows; i++)
            {
                for (int j = 1; j <= noOfCols; j++)
                {
                    string seat = (Char)('A' + i) + "" + j;
                    sb.Append("<seat status=\"Available\" reservedBy=\"\">");
                    sb.Append(seat);
                    sb.Append("</seat>");
                }
            }
            sb.Append("</seatLayout>");
            return sb.ToString();
        }

        public bool DeleteMovieScheduleOfSpecificMovie(int MovieID)
          {

              List<MovieSchedule> movieScheduleList = GetMovieScheduleDALByMovie(MovieID);

              foreach (MovieSchedule ms in movieScheduleList)
            {
                Delete(ms);
            }

              return true;
          }

        public bool Delete()
        {
            MovieSchedule movieSchedule = new MovieSchedule();
            movieSchedule.CinemaID = this.CinemaID;
            movieSchedule.EndDate = this.EndDate;
            movieSchedule.IsDeleted = true;
            movieSchedule.MovieID = this.MovieID;
            movieSchedule.MovieScheduleID = this.MovieScheduleID;
            movieSchedule.ScreenNo = this.ScreenNo;
            movieSchedule.ShowEndTime = this.ShowEndTime;
            movieSchedule.ShowStartTime = this.ShowStartTime;
            movieSchedule.StartDate = this.StartDate;
            movieSchedule.WeekDateFare = Convert.ToDecimal(this.WeekDateFare);
            movieSchedule.WeekEndFare = Convert.ToDecimal(this.WeekEndFare);
            mbs.AddToMovieSchedules(movieSchedule);
            mbs.SaveChanges();

            ShowTimeBE showTimeBE = new ShowTimeBE();
            showTimeBE.DeleteShowTimeOfSpecificMovieSchedule(movieSchedule.MovieScheduleID);

            return true;
        }

        public bool Update()
        {
            MovieSchedule movieSchedule = GetMovieScheduleDALByID(this.MovieScheduleID);
            movieSchedule.CinemaID = this.CinemaID;
            movieSchedule.EndDate = this.EndDate;
            movieSchedule.IsDeleted = this.IsDeleted;
            movieSchedule.MovieID = this.MovieID;
            movieSchedule.MovieScheduleID = this.MovieScheduleID;
            movieSchedule.ScreenNo = this.ScreenNo;
            movieSchedule.ShowEndTime = this.ShowEndTime;
            movieSchedule.ShowStartTime = this.ShowStartTime;
            movieSchedule.StartDate = this.StartDate;
            movieSchedule.WeekDateFare = Convert.ToDecimal(this.WeekDateFare);
            movieSchedule.WeekEndFare = Convert.ToDecimal(this.WeekEndFare);
            mbs.AddToMovieSchedules(movieSchedule);
            mbs.SaveChanges();
            return true;
        }


        private bool Delete(MovieSchedule movieSchedule)
        {
            if (movieSchedule == null)
            {
                return false;
            }


            if (movieSchedule.IsDeleted)
            {
                return false;
            }

            movieSchedule.IsDeleted = true;

            List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.MovieScheduleID == MovieScheduleID).ToList();
            foreach (ShowTime showTime in showTimeList)
            {
                showTime.IsDeleted = true;
            }


            mbs.SaveChanges();

            return true;
        }

        public bool Delete(int MovieScheduleID)
        {

            MovieSchedule movieSchedule = GetMovieScheduleDALByID(MovieScheduleID);

            return Delete(movieSchedule);
        }

        private MovieScheduleBE ConvertDALToBusinessEntity(MovieSchedule movieScheduleDAL)
        {

            MovieScheduleBE movieScheduleBE = new MovieScheduleBE();
            movieScheduleBE.CinemaID = movieScheduleDAL.CinemaID;
            movieScheduleBE.EndDate = Convert.ToDateTime(movieScheduleDAL.EndDate);
            movieScheduleBE.IsDeleted = Convert.ToBoolean(movieScheduleDAL.IsDeleted);
            movieScheduleBE.MovieID = movieScheduleDAL.MovieID;
            movieScheduleBE.MovieScheduleID = movieScheduleDAL.MovieScheduleID;
            movieScheduleBE.ScreenNo = movieScheduleDAL.ScreenNo;
          //  DateTime endTime = Convert.ToDateTime(movieScheduleDAL.ShowEndTime);
          // DateTime startTime = Convert.ToDateTime(movieScheduleDAL.ShowStartTime);
          // movieScheduleBE.ShowEndTime = new TimeSpan(endTime.Hour, endTime.Minute, endTime.Second);
          // movieScheduleBE.ShowStartTime = new TimeSpan(startTime.Hour, startTime.Minute, startTime.Second);
            movieScheduleBE.ShowStartTime = (TimeSpan)movieScheduleDAL.ShowStartTime;
            movieScheduleBE.ShowEndTime = (TimeSpan)movieScheduleDAL.ShowEndTime; ;
            movieScheduleBE.StartDate = Convert.ToDateTime(movieScheduleDAL.StartDate);
            movieScheduleBE.WeekDateFare = Convert.ToDouble(movieScheduleDAL.WeekDateFare);
            movieScheduleBE.WeekEndFare = Convert.ToDouble(movieScheduleDAL.WeekEndFare);

            return movieScheduleBE;
        }

        private List<MovieSchedule> GetAllDALShowSchedules()
        {
            return mbs.MovieSchedules.ToList();
        }



        // by movieScheduleID
        public MovieScheduleBE GetMovieScheduleByID(int MovieScheduleID)
        {
            MovieScheduleBE movieScheduleBE = new MovieScheduleBE();
            MovieSchedule movieSchedule = GetMovieScheduleDALByID(MovieScheduleID);
            movieScheduleBE = ConvertDALToBusinessEntity(movieSchedule);
            return movieScheduleBE;
        }

        public object GetByID(int MovieScheduleID)
        {
            MovieScheduleBE movieScheduleBE = new MovieScheduleBE();
            MovieSchedule movieSchedule = GetMovieScheduleDALByID(MovieScheduleID);
            movieScheduleBE = ConvertDALToBusinessEntity(movieSchedule);
            if (Validate_Retrieval(movieScheduleBE)) { return movieScheduleBE; }
            return null;
        }

        private bool Validate_Retrieval(MovieScheduleBE movieScheduleBE)
        {
            if (movieScheduleBE == null) { isValid = false; return false; }

            return true;
        }

        private MovieSchedule GetMovieScheduleDALByID(int MovieScheduleID)
        {
            MovieSchedule movieScheduleDAL = new MovieSchedule();
            movieScheduleDAL = mbs.MovieSchedules.First(x => x.MovieScheduleID == MovieScheduleID);
            return movieScheduleDAL;

        }


        // by movie, cinema, & screen ID
        private List<MovieSchedule> GetMovieScheduleDALByMovieAndCinemaAndTheatreNo(int MovieID, int CinemaID, int ScreenNo)
        {
            List<MovieSchedule> movieScheduleList = mbs.MovieSchedules.Where(m => m.MovieID == MovieID && m.CinemaID == CinemaID && m.ScreenNo == ScreenNo && m.IsDeleted == false).ToList();

            return movieScheduleList;
        }

        public List<MovieScheduleBE> GetMovieScheduleByMovieAndCinemaAndTheatreNo(int MovieID, int CinemaID, int ScreenNo)
        {
            List<MovieSchedule> movieScheduleList = GetMovieScheduleDALByMovieAndCinemaAndTheatreNo(MovieID, CinemaID, ScreenNo);
            List<MovieScheduleBE> movieScheduleBEList = new List<MovieScheduleBE>();
            foreach (MovieSchedule movieSchedule in movieScheduleList)
            {
                MovieScheduleBE movieScheduleBE = ConvertDALToBusinessEntity(movieSchedule);
                movieScheduleBEList.Add(movieScheduleBE);
            }
            return movieScheduleBEList;

        }

        // by cinema & screen ID
        private List<MovieSchedule> GetMovieScheduleDALByCinemaAndTheatreNo(int CinemaID, int ScreenNo)
        {
            List<MovieSchedule> movieScheduleList = mbs.MovieSchedules.Where(m => m.CinemaID == CinemaID && m.ScreenNo == ScreenNo && m.IsDeleted == false).ToList();

            return movieScheduleList;
        }

        public List<MovieScheduleBE> GetMovieScheduleByCinemaAndTheatreNo(int CinemaID, int ScreenNo)
        {
            List<MovieSchedule> movieScheduleList = GetMovieScheduleDALByCinemaAndTheatreNo(CinemaID, ScreenNo);
            List<MovieScheduleBE> movieScheduleBEList = new List<MovieScheduleBE>();
            foreach (MovieSchedule movieSchedule in movieScheduleList)
            {
                MovieScheduleBE movieScheduleBE = ConvertDALToBusinessEntity(movieSchedule);
                movieScheduleBEList.Add(movieScheduleBE);
            }
            return movieScheduleBEList;

        }

        // by movie ID

        private List<MovieSchedule> GetMovieScheduleDALByMovie(int MovieID)
        {
            List<MovieSchedule> movieScheduleList = mbs.MovieSchedules.Where(m => m.MovieID == MovieID && m.IsDeleted == false).ToList();

            return movieScheduleList;
        }

        public List<MovieScheduleBE> GetMovieScheduleByMovie(int MovieID)
        {
            List<MovieSchedule> movieScheduleList = GetMovieScheduleDALByMovie(MovieID);
            List<MovieScheduleBE> movieScheduleBEList = new List<MovieScheduleBE>();
            foreach (MovieSchedule movieSchedule in movieScheduleList)
            {
                MovieScheduleBE movieScheduleBE = ConvertDALToBusinessEntity(movieSchedule);
                movieScheduleBEList.Add(movieScheduleBE);
            }
            return movieScheduleBEList;

        }


        // by cinema ID

        private List<MovieSchedule> GetMovieScheduleDALByCinema(int CinemaID)
        {
            List<MovieSchedule> movieScheduleList = mbs.MovieSchedules.Where(m => m.CinemaID == CinemaID && m.IsDeleted == false).ToList();

            return movieScheduleList;
        }

        public List<MovieScheduleBE> GetMovieScheduleByCinema(int CinemaID)
        {
            List<MovieSchedule> movieScheduleList = GetMovieScheduleDALByMovieAndCinemaAndTheatreNo(MovieID, CinemaID, ScreenNo);
            List<MovieScheduleBE> movieScheduleBEList = new List<MovieScheduleBE>();
            foreach (MovieSchedule movieSchedule in movieScheduleList)
            {
                MovieScheduleBE movieScheduleBE = ConvertDALToBusinessEntity(movieSchedule);
                movieScheduleBEList.Add(movieScheduleBE);
            }
            return movieScheduleBEList;

        }

        #endregion



    }
}
